import {
  PanelGroupKey,
  ScanBoardKey,
  SendboxKey,
  V3proAlertMessageFile,
} from "../model/v3pro.model";
import { computeHash, createTimestampFilename, createTmpFile } from "../../../utils/file.util";
import { SRFileTypeEnum } from "../../starriver-task/model/sr.enum";
import {
  SRExternalDeviceFile,
  SRJSONFile,
  SRPanelGroupFile,
  SRScanBoardFile,
  SRSendBoxFile,
} from "../model/v3pro.file.model";
import { V3proExternalDeviceStatus } from "../../../api/v3pro-api/v3pro.api.response.model";

/**
 * V3pro文件助手
 */
export class V3proFileHelper {
  /**
   * 将 srJSON 转换为文件
   * @param srJson
   * @param type
   */
  static readonly srJson2File = async (
    srJson: SRJSONFile,
    type: SRFileTypeEnum = SRFileTypeEnum.SCREEN_ERROR,
  ): Promise<V3proAlertMessageFile> => {
    const file = createTmpFile(JSON.stringify(srJson), "json");
    return new V3proAlertMessageFile(
      type,
      file,
      await computeHash(file.name),
      createTimestampFilename("screen-error", "json"),
    );
  };

  /**
   * 发送盒关键信息 转换为文件
   * @param layout
   * @param type 文件类型
   * @param keys 发送盒IP
   */
  static readonly sendBoxKey2File = async (
    keys: SendboxKey[],
    layout?: any[],
    type: SRFileTypeEnum = SRFileTypeEnum.SCREEN_ERROR,
  ): Promise<V3proAlertMessageFile> => {
    const file = createTmpFile(JSON.stringify(new SRSendBoxFile(keys, layout)), "json");
    return new V3proAlertMessageFile(
      type,
      file,
      await computeHash(file.name),
      createTimestampFilename("sendbox", "json"),
    );
  };

  /**
   * 扫描板关键信息 转换为文件
   * @param layout
   * @param type
   * @param keys
   */
  static readonly scanBoardKey2File = async (
    keys: ScanBoardKey[],
    layout?: any[],
    type: SRFileTypeEnum = SRFileTypeEnum.SCREEN_ERROR,
  ): Promise<V3proAlertMessageFile> => {
    const file = createTmpFile(JSON.stringify(new SRScanBoardFile(keys, layout)), "json");
    return new V3proAlertMessageFile(
      type,
      file,
      await computeHash(file.name),
      createTimestampFilename("scanboard", "json"),
    );
  };

  /**
   * 面板关键信息 转换为文件
   * @param layout
   * @param type
   * @param keys
   */
  static readonly panelGroupKey2File = async (
    keys: PanelGroupKey[],
    layout?: any[],
    type: SRFileTypeEnum = SRFileTypeEnum.SCREEN_ERROR,
  ): Promise<V3proAlertMessageFile> => {
    const file = createTmpFile(JSON.stringify(new SRPanelGroupFile(keys, layout)), "json");
    return new V3proAlertMessageFile(
      type,
      file,
      await computeHash(file.name),
      createTimestampFilename("panel-group", "json"),
    );
  };

  /**
   * 外部设备关键信息 转换为文件
   * @param layout
   * @param type
   * @param keys
   */
  static readonly externalDeviceKey2File = async (
    keys: V3proExternalDeviceStatus[],
    layout?: any[],
    type: SRFileTypeEnum = SRFileTypeEnum.SCREEN_ERROR,
  ): Promise<V3proAlertMessageFile> => {
    const file = createTmpFile(JSON.stringify(new SRExternalDeviceFile(keys, layout)), "json");
    return new V3proAlertMessageFile(
      type,
      file,
      await computeHash(file.name),
      createTimestampFilename("external-device", "json"),
    );
  };
}
